﻿package classes {
	import flash.display.MovieClip;
	import classes.Events.*;
	import classes.*;

	public class MainEngine extends MovieClip {
 
        public var mainMenu:MainMenu;
		public var mario:Mario;
		public var gstage:Level1;
		public var gameMode:String;
		public var fixedObjects:Array;
		public var otherObjects:Array;
		
		
		public function MainEngine() {
			mainMenu = new MainMenu();
			stage.addChild(mainMenu);
			mainMenu.addEventListener(ButtonEvent.PLAY, runGame);
			gameOpened();
			
		}
		function gameOpened(){
			// This Function is Used to set the default vars at the beggining of the game.
			gameMode = "play";
		}
		function runGame(e:ButtonEvent){
			
			
			createStage();
			createMario();
			observeLevel();
			gameMode = "play";
			/*
			
			*/
		}
		function createMario(){
			mario = new Mario();
			mario.x = 100;
			mario.y = 300;
			gstage.addChild(mario);
			trace("Adding Mario to the Game.\n");
			stage.removeChild(mainMenu);
			
		}
		function createStage(){
			gstage = new Level1();
			stage.addChild(gstage);
			gstage.gotoAndStop(1);
		}
		function checkCollision(){
			
		}
		private function traceObject(o:Object):void{
	trace('\n');
	for(var val:* in o){
		trace('   [' + typeof(o[val]) + '] ' + val + ' => ' + o[val]);
	}
	trace('\n');
}
		function observeLevel(){
			var fixedObjects:Array = new Array();
			var otherObjects:Array = new Array();
			
			for(var i:int = 0; i < gstage.numChildren; i++)
			{
				var ic = gstage.getChildAt(i); 
				  if((ic is GroundTile) ||(ic is GroundTile2) || (ic is GroundTile3)){
					  var groundObject:Object = new Object();
					  groundObject.ic = ic;
					  groundObject.leftside = ic.x;
					  groundObject.rightside = ic.x+ic.width;
					  groundObject.topside = ic.y;
					  groundObject.bottomside = ic.y+ic.height;
					  fixedObjects.push(groundObject);
					  trace(fixedObjects.length+"\n");//Count objects..
					  traceObject(groundObject);
					  
				  }else if(ic is Mario){
						var character:Object = new Object();
						character.ic = ic;
					   character.leftside = ic.x;
					   character.rightside = ic.x+ic.width;
					   character.topside = ic.y;
					   character.bottomside = ic.y+ic.height;
					   fixedObjects.push(character);
					   traceObject(character);
					   
				  }else if((ic is ABox)){
					  var Ammo:Object = new Object();
					  Ammo.box = ic;
					  Ammo.leftside = ic.x;
					  Ammo.rightside = ic.x + ic.width;
					  Ammo.topside = ic.y;
					  Ammo.bottomside = ic.y + ic.height;
					  otherObjects.push(Ammo);
					  traceObject(Ammo);
					  
				  }else if((ic is Vodka) || (ic is Cocain) || (ic is Weed)){
					  var powerUp:Object = new Object();
					  powerUp.special = ic;
					  powerUp.leftside = ic.x;
					  powerUp.rightside = ic.x + ic.width;
					  powerUp.topside = ic.y;
					  powerUp.bottomside = ic.y + ic.height;
					  otherObjects.push(powerUp);
					  traceObject(powerUp);
				  }   
					  
					  //Other Type of Objects will go here
				  }
			}
		}

	
}
